package controllers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import model.entities.ChubConsInvoiceInfo;
import myDaos.ChubInvoicesDao;
import myDaos.ChubStocksDao;

@ManagedBean
@ViewScoped
public class ChubInvoicesManagedBean {
	private String region;
	private String distributor;
	private String country;
	private Date from;
	private Date to;
	private List<ChubConsInvoiceInfo> iList;
	private boolean isProcedureCalled = false;
	private List<String> distributors;
	private List<SelectItem> distList;
	private List<SelectItem> countriesList;
	private List<String> countries;
	
	@EJB
	private ChubInvoicesDao cid;
	
	@ManagedProperty("#{servicesManagedBean}")
	private ServicesManagedBean servicesManagedBean;
	
	public List<SelectItem> getCountriesList() {
		return countriesList;
	}
	public void setCountriesList(List<SelectItem> countriesList) {
		this.countriesList = countriesList;
	}
	public List<String> getCountries() {
		return countries;
	}
	public void setCountries(List<String> countries) {
		this.countries = countries;
	}
	public String getRegion() {
		return region;
	}
	public void setRegion(String region) {
		this.region = region;
	}
	public ServicesManagedBean getServicesManagedBean() {
		return servicesManagedBean;
	}
	public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
		this.servicesManagedBean = servicesManagedBean;
	}
	public List<String> getDistributors() {
		return distributors;
	}
	public void setDistributors(List<String> distributors) {
		this.distributors = distributors;
	}
	public List<SelectItem> getDistList() {
		return distList;
	}
	public void setDistList(List<SelectItem> distList) {
		this.distList = distList;
	}
	public String getDistributor() {
		return distributor;
	}
	public void setDistributor(String distributor) {
		this.distributor = distributor;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public Date getFrom() {
		return from;
	}
	public void setFrom(Date from) {
		this.from = from;
	}
	public Date getTo() {
		return to;
	}
	public void setTo(Date to) {
		this.to = to;
	}
	public List<ChubConsInvoiceInfo> getiList() {
		return iList;
	}
	public void setiList(List<ChubConsInvoiceInfo> iList) {
		this.iList = iList;
	}
	public boolean isProcedureCalled() {
		return isProcedureCalled;
	}
	public void setProcedureCalled(boolean isProcedureCalled) {
		this.isProcedureCalled = isProcedureCalled;
	}
	public void callChubInvoicesProcedure(){
		Boolean procedureReturn;
		procedureReturn=cid.callChubInvoicesProcedure(region,country,distributor,from,to);
		if(procedureReturn)
		{
			isProcedureCalled=true;
			getInvoicesList();
		}
	}

	public void getInvoicesList()
	{
		iList=cid.getChubInvoicesList();
	}
	
	public void updateCountries()
	{
		//Proposed by Samy
		countriesList = new ArrayList<SelectItem>();
		countries = new ArrayList<String>();
		for(int i = 0; i < servicesManagedBean.getCountrieslist().size(); i++) 
	    {  
			if(region!=null && !region.trim().equals("") && 
					  servicesManagedBean.getCountrieslist().get(i).getMapRegion().equals(region))
			{
				if(!countries.contains(servicesManagedBean.getCountrieslist().get(i).getMapCountry())){
					countries.add(servicesManagedBean.getCountrieslist().get(i).getMapCountry());
					countriesList.add(new SelectItem(servicesManagedBean.getCountrieslist().get(i).getMapCountry(),
							servicesManagedBean.getCountrieslist().get(i).getMapCountry()));
				}
			}
	    }
	}
	
	public void updateDistributorsList()
	{
		//Proposed by Samy
		distList = new ArrayList<SelectItem>();
		for(int i = 0; i < servicesManagedBean.getCountriesDistributorList().size(); i++) 
	    {  
			if(country!=null && !country.trim().equals("") && 
					  servicesManagedBean.getCountriesDistributorList().get(i).getCountry().equals(country))
			{
			distList.add(new SelectItem(
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
										 ,
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
					                    )
			             );
			}
	    }
	}
	
	@PostConstruct
	public void init(){
		to = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(to);
		gc.add(Calendar.DAY_OF_YEAR, -1);
		from= gc.getTime();
	}
}
